package com.xunshangwang.pinhaohuo.ui.activities;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.xunshangwang.pinhaohuo.R;
import com.xunshangwang.pinhaohuo.adapters.GroupAdapter;
import com.xunshangwang.pinhaohuo.decorations.DividerItemDecoration;
import com.xunshangwang.pinhaohuo.decorations.OffsetItemDecoration;
import com.xunshangwang.pinhaohuo.entity.Group;
import com.xunshangwang.pinhaohuo.http.HttpTool;
import com.xunshangwang.pinhaohuo.http.JsonHttpHandler;
import com.xunshangwang.pinhaohuo.widgets.LMSizeRecyclerView;
import com.xunshangwang.pinhaohuo.widgets.RefreshLayout;

import org.json.JSONObject;

import java.util.List;

import cz.msebera.android.httpclient.Header;
import tool.library.adapter.BaseRecyclerAdapter;
import tool.library.widget.LoadRecyclerView;

/**
 * Created by xzp on 16/3/8.
 */
public class GroupListActivity extends BaseActivity implements BaseRecyclerAdapter.OnItemClickListener,SwipeRefreshLayout.OnRefreshListener,
        LoadRecyclerView.OnLoadListener{
    private LMSizeRecyclerView mRecycler;
    private GroupAdapter mAdapter;
    private RefreshLayout mRefresh;
    private String mGoodsId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);
        mGoodsId=getIntent().getStringExtra("goodsId");
        mRecycler= (LMSizeRecyclerView) this.findViewById(R.id.group_list);
        mRefresh= (RefreshLayout) this.findViewById(R.id.refresh_layout);
        mRecycler.setLayoutManager(new LinearLayoutManager(this));
        mRecycler.addItemDecoration(new DividerItemDecoration(this) {
            @Override
            protected boolean canDraw(int position) {
                return position < mAdapter.getItemCount() - 1;
            }
        });
        mRecycler.addItemDecoration(new OffsetItemDecoration(this, R.dimen.group_list_item_offset) {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                if (parent.getChildAdapterPosition(view) < parent.getAdapter().getItemCount() - 2)
                    super.getItemOffsets(outRect, view, parent, state);
            }
        });
        mAdapter=new GroupAdapter(this);
        mAdapter.setOnItemClickListener(this);
        mRecycler.setAdapter(mAdapter);
        mRecycler.setOnLoadListener(this);
        mRefresh.setOnRefreshListener(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
                onRefresh();
            }
        },150);
    }


    @Override
    public void onItemClick(BaseRecyclerAdapter adapter, View item, int position) {
        Intent intent=new Intent(this,GroupDetailActivity.class);
        intent.putExtra("id",mAdapter.getList().get(position).getGroup_order_id());
        startActivity(intent);
    }

    @Override
    public void onLoad(LoadRecyclerView recyclerView) {
        requestLoad();
    }

    @Override
    public void onRefresh() {
        requestRefresh();
    }
    private void requestRefresh(){
        StringBuilder builder=new StringBuilder("?g=Api&m=Goods&a=grouping");
        builder.append("&goods_id="+mGoodsId);
        builder.append("&offset=0");
        builder.append("&size="+LMSizeRecyclerView.SIZE);
        HttpTool.get(this, builder.toString(), new JsonHttpHandler<List<Group>>() {
            @Override
            public List<Group> parseData(JSONObject object) throws Throwable {
                return JSON.parseArray(object.getString("goods"), Group.class);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mRefresh.setRefreshing(false);
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, List<Group> response) {
                mAdapter.setList(response);
                mRecycler.loadFinish(response);
            }
        });
    }
    private void requestLoad(){
        StringBuilder builder=new StringBuilder("?g=Api&m=Goods&a=grouping");
        builder.append("&goods_id="+mGoodsId);
        builder.append("&offset="+mAdapter.getListSize());
        builder.append("&size="+LMSizeRecyclerView.SIZE);
        HttpTool.get(this, builder.toString(), new JsonHttpHandler<List<Group>>() {
            @Override
            public List<Group> parseData(JSONObject object) throws Throwable {
                return JSON.parseArray(object.getString("goods"),Group.class);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable) {
                super.onFailure(statusCode, headers, responseBytes, throwable);
                mRecycler.loadFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, List<Group> response) {
                mAdapter.addAll(response);
                mRecycler.loadFinish(response);
            }
        });
    }
}
